package com.prs.crm.action;


import java.io.File;

import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.courser.struts.BaseAction;
import org.courser.ui.PaginationSupport;
import com.prs.crm.annotation.Clew;
import com.prs.crm.context.AppContext;
import com.prs.crm.context.Attach;
import com.prs.crm.context.FileUtil;
import com.prs.crm.domain.Attachment;
import com.prs.crm.domain.hr.Person;
import com.prs.crm.service.hr.PersonService;
import com.prs.crm.service.hr.ResumeService;


public class SetInfoAction extends BaseAction {
	private static final long serialVersionUID = -6136720957125121468L;
	private Person person;
	private PersonService personService;
	private ResumeService resumeService;
	private File upload;
	private String uploadFileName;
	private PaginationSupport pagination;
	private String oldPwd;
	private String affirmPwd;
	private String newPwd;

	@Action("editSetInfo")
	public String edit() {
		person = AppContext.getAccountContext().currentUser();
		return SUCCESS;
	}
	

	@Clew(results={"success"})
	@Action(value = "saveSetInfo", results = { @Result(name = "success", type = "redirect", location = "editSetInfo"),
			@Result(name="error",type="chain",location="editSetInfo")})
	public String save(){		
		if(newPwd != null && !newPwd.equals("")) {
		
			if(newPwd == null) {
				this.addActionError("请输入原密码");
				return ERROR;
			}			
			if(newPwd.length() < 4) {
				this.addActionError("密码长度应大于4位");
				return ERROR;
			}
			if(!oldPwd.equals(person.getLoginPwd())) {
				this.addActionError("密码错误");
				return ERROR;
			}
			if(!newPwd.equals(affirmPwd)) {
				this.addActionError("新密码与确认密码不一致");
				return ERROR;
			}
			person.setLoginPwd(newPwd);
		}
		if (upload!=null) {
			Attachment att=FileUtil.getInstance().createAttach(upload, uploadFileName);
			person.getResume().setPhotoImg(att.getPath());
		}
		this.setPrompt("修改个人设置成功");
		this.getPersonService().save(person);
//		this.getResumeService().submitAlterPerson(person.getResume(), upload, uploadFileName);
		return SUCCESS;
	}
	public Person getPerson() {
		return person;
	}


	public void setPerson(Person person) {
		this.person = person;
	}


	public PersonService getPersonService() {
		return personService;
	}


	public void setPersonService(PersonService personService) {
		this.personService = personService;
	}

	public ResumeService getResumeService() {
		return resumeService;
	}


	public void setResumeService(ResumeService resumeService) {
		this.resumeService = resumeService;
	}


	public static long getSerialVersionUID() {
		return serialVersionUID;
	}

	public File getUpload() {
		return upload;
	}

	public void setUpload(File upload) {
		this.upload = upload;
	}

	public String getUploadFileName() {
		return uploadFileName;
	}

	public void setUploadFileName(String uploadFileName) {
		this.uploadFileName = uploadFileName;
	}

	public void setOldPwd(String oldPwd) {
		this.oldPwd = oldPwd;
	}

	public String getOldPwd() {
		return oldPwd;
	}

	public void setAffirmPwd(String affirmPwd) {
		this.affirmPwd = affirmPwd;
	}

	public String getAffirmPwd() {
		return affirmPwd;
	}

	public void setNewPwd(String newPwd) {
		this.newPwd = newPwd;
	}

	public String getNewPwd() {
		return newPwd;
	}

	
	public PaginationSupport getPagination() {
		return pagination;
	}

	public void setPagination(PaginationSupport pagination) {
		this.pagination = pagination;
	}

	

	public static long getSerialversionuid() {
		return serialVersionUID;
	}

}
